package com.example.book_libapp;

import java.util.ArrayList;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class BookListBaseAdapter extends BaseAdapter {
	private static ArrayList<Book> BookrrayList;
	
	private Integer[] imgid = {
			R.drawable.pic_1,
			R.drawable.pic_2,
			R.drawable.pic_3,
			R.drawable.pic_4,
			R.drawable.pic_5,
			R.drawable.pic_6
			};
	
	private LayoutInflater l_Inflater;

	public BookListBaseAdapter(Context context, ArrayList<Book> results) {
		BookrrayList = results;
		l_Inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return BookrrayList.size();
	}

	public Object getItem(int position) {
		return BookrrayList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = l_Inflater.inflate(R.layout.book_listdetails, null);
			holder = new ViewHolder();
			holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
			holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.cityState);
			holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.phone);
			holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
			//holder.rate = (RatingBar) convertView.findViewById(R.id.rating);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.txt_itemName.setText(BookrrayList.get(position).getName());
		holder.txt_itemDescription.setText(BookrrayList.get(position).getAuthor());
		holder.txt_itemPrice.setText(BookrrayList.get(position).getPrice());
		holder.itemImage.setImageResource(imgid[BookrrayList.get(position).getImageNumber() - 1]);
      
		return convertView;
	}

	static class ViewHolder {
		TextView txt_itemName;
		TextView txt_itemDescription;
		TextView txt_itemPrice;
		ImageView itemImage;
		//RatingBar rate;
	}
}
